#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    mainpage *homepage = new mainpage(ui->stackedWidget);
    ui->stackedWidget->addWidget(homepage);
    ui->stackedWidget->setCurrentWidget(homepage);
    appCache[homeApp] = homepage;
    connect(homepage,&mainpage::switchToPage,this,&MainWindow::onPageSwitch);
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::onPageSwitch(AppType index)
{
    if(appCache.contains(index))
    {
        QWidget *targetApp = appCache[index];
        ui->stackedWidget->setCurrentWidget(targetApp);
        return;
    }

    QWidget *newApp = nullptr;
    switch(index)
    {
    case homeApp:
        newApp = new mainpage(ui->stackedWidget);
        break;
    case cameraApp:
        newApp = new camera_App(ui->stackedWidget);
        break;
    case photoApp:
        newApp = new photo_App(ui->stackedWidget);
        break;
    case musicApp:
        newApp = new music_App(ui->stackedWidget);
        break;
    case videoApp:
        newApp = new video_App(ui->stackedWidget);
        break;
    case mapApp:
        newApp = new map_App(ui->stackedWidget);
        break;
    case clockApp:
        newApp = new clock_App(ui->stackedWidget);
        break;
    case settingApp:
        newApp = new setting_App(ui->stackedWidget);
        break;
    case messageApp:
        newApp = new message_App(ui->stackedWidget);
        break;
    }

    if(newApp)
    {
        ui->stackedWidget->addWidget(newApp);
        appCache[index] = newApp;
        ui->stackedWidget->setCurrentWidget(newApp);
    }
    return;
}



void MainWindow::on_backButton_clicked()
{
    ui->stackedWidget->setCurrentWidget(appCache[homeApp]);
}

